package sword_to_2;

import java.util.HashSet;
import java.util.Set;

public class LeeCode_5 {
    public static void main(String[] args) {

    }
    private static int maxProduct2(String[] words) {
        int n = words.length;
        int[] dp = new int[n];
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            int num = 0;
            for (int j = 0; j < word.length(); j++) {
                char c = word.charAt(j);
                num |= 1 << (c - 'a');
            }
            dp[i] = num;
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if ((dp[i] & dp[j]) == 0){
                    ans = Math.max(words[i].length() * words[j].length(), ans);
                }
            }
        }
        return ans;
    }
    private static int maxProduct(String[] words) {
        int n = words.length;
        Set<Character>[] sets = new Set[n];
        boolean[][] dp = new boolean[n][n];
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            sets[i] = new HashSet<>();
            for (int j = 0; j < word.length(); j++) {
                char c = word.charAt(j);
                for (int k = 0; k < i; k++) {
                    if (sets[k].contains(c)){
                        dp[k][i] = true;
                    }
                }
                sets[i].add(c);
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (!dp[i][j]){
                    ans = Math.max(words[i].length() * words[j].length(), ans);
                }
            }
        }
        return ans;
    }
}
